import treeUtil from '../treeUtil'
/**
 * @author liubaohui
 * @deprecated 已修改为后端过滤
 * @desc 工单或任务号搜索
 */
let workOrderBomTreeSearch = {
  methods: {
    handleSearch() {
      if (this.keyword === '') return
      let nodesMap = this.$refs.elTree.store.nodesMap
      let node = nodesMap[this.keyword.toUpperCase()]
      // level = 2的是任务号
      if (node === undefined || node.level !== 2) {
        return this.searchWorkorder()
      }
      this.searchTaskNumber(node)
    },
    searchTaskNumber(node) {
      // 设置高亮并展开任务号的父节点
      this.$refs.elTree.setCurrentNode(node)
      node.parent.expanded = true
      let elLabelList = this.$refs.elTree.$el.getElementsByClassName('label_taskNumber')
      let el = Array.from(elLabelList).find(label => label.innerText === this.keyword.toUpperCase())
      el.parentNode.parentNode.parentNode.parentNode.children[0].click()
      let timer = setInterval(() => {
        let offsetTop = el.offsetTop - 4
        let clientHeight = this.$refs.scrollbar.$refs.wrap.clientHeight
        if (this.treeClientHeight === clientHeight) {
          this.$nextTick(() => {
            this.$refs.scrollbar.$refs.wrap.scrollTop = offsetTop - 4
          })
          return clearInterval(timer)
        }
        this.treeClientHeight = clientHeight
      }, 50)
    },
    async searchWorkorder() {
      // 先根据工单号查找任务号
      let data = await this.getTaskNumber()
      let taskNumber = data.taskNumber
      if (taskNumber === '') {
        return this.$message.error({
          message: '未搜索到该工单或任务号',
          duration: 1500,
          center: true
        })
      }
      // 根据任务号获取工单树，插入到对应的任务号节点下
      let taskNode = this.$refs.elTree.store.nodesMap[taskNumber]
      taskNode.data.children = await this.getWorkOrderTree(taskNumber)
      if (!Array.isArray(taskNode.data.children)) {
        return this.$message.error({
          message: '未搜索到该工单或任务号',
          duration: 1500,
          center: true
        })
      }
      this.$nextTick(() => {
        // 从树中查找匹配的工单号的节点id
        let matchedNode = this.getMatchedNode()
        this.targetId = matchedNode.id
        if (this.targetId === null) {
          return this.$message.error({
            message: '未搜索到该工单',
            duration: 1500,
            center: true
          })
        }
        // 找到匹配的工单号后展开任务号并定位到工单元素位置
        this.scrollToMatchedView(taskNode)
        // 抛出selectNode事件。对于齐套信息查询需要抛出：1、选中的工单id，2、选中的工单编号，3、节点级别
        let matchedWorkorder = matchedNode.data.workorders.find(w => w.number === this.keyword.toUpperCase())
        this.$emit('selectNode', matchedWorkorder.id, matchedWorkorder.number, matchedNode.level, matchedNode.data.name)
      })
    },
    // 从树中查找匹配的工单号对应的节点
    getMatchedNode() {
      let nodes = Object.values(this.$refs.elTree.store.nodesMap)
      return nodes.find(n => {
        let workorders = n.data.workorders
        return !!(Array.isArray(workorders) && workorders.find(w => w.number === this.keyword.toUpperCase()) !== undefined)
      }) || {id: null}
    },
    // 找到匹配的工单号后展开任务号并定位到工单元素位置
    scrollToMatchedView(taskNode) {
      taskNode.expanded = true
      treeUtil.expandChildNodes(taskNode)
      this.$refs.scrollbar.$refs.wrap.scrollTop = 0
      // 展开任务号后定位到元素位置
      let timer = setInterval(() => {
        let offsetTop = this.$el.querySelector('.tree_search_result').offsetTop
        let clientHeight = this.$refs.elTree.$el.clientHeight
        if (this.treeClientHeight === clientHeight) {
          this.$nextTick(() => {
            this.$refs.scrollbar.$refs.wrap.scrollTop = offsetTop - 4
          })
          return clearInterval(timer)
        }
        this.treeClientHeight = clientHeight
      }, 50)
    },
  }
}

export default workOrderBomTreeSearch
